﻿namespace GoodAdmin.System;

public class ExtraPosService : IExtraPosService, ITransient
{
    private readonly IBaseService _baseService;
    private readonly ISysPositionService _sysPositionService;

    public ExtraPosService(IBaseService baseService,ISysPositionService sysPositionService)
    {
        _baseService = baseService;
        _sysPositionService = sysPositionService;
    }
    /// <inheritdoc />
    public async Task AddPosition(PositionAddInput input)
    {
        await CheckDataScope(new List<long> { input.OrgId });
        await _sysPositionService.AddPosition(input);
    }
    /// <inheritdoc />
    public async Task DeletePosition(List<BaseIdInput> input)
    {
        var inputIdList = input.Select(it=>it.Id).ToList();
        var posList = await _sysPositionService.GetPositionList();
        var orgIdList = posList.Where(it => inputIdList.Contains(it.Id)).Select(it=>it.OrgId).ToList();
        await CheckDataScope(orgIdList);

        await _sysPositionService.DeletePosition(input);
    }
    /// <inheritdoc />
    public async Task<SqlSugarPagedList<SysPosition>> GetPositionPageList(PositionPageInput input)
    {
        if (!UserManager.SuperAdmin)
        {
            var dataScope = await _baseService.GetUserDataScopeByRequest();
            input.DataScope = dataScope;
        }
        return await _sysPositionService.GetPositionPageList(input);
    }
    /// <inheritdoc />
    public async Task UpdatePosition(PositionUpdateInput input)
    {
        await CheckDataScope(new List<long> { input.OrgId });
        await _sysPositionService.UpdatePosition(input);
    }

    /// <inheritdoc />
    public async Task<List<SysPosition>> GetPositionList(PositionSelectorInput input)
    {
        if (!UserManager.SuperAdmin)
        {
            var dataScope = await _baseService.GetUserDataScopeByRequest();
            input.DataScope = dataScope;
        }
        return await _sysPositionService.GetPositionList(input);
    }


    /// <summary>
    /// 检查数据范围
    /// </summary>
    /// <param name="orgIds"></param>
    /// <returns></returns>
    private async Task CheckDataScope(List<long> orgIds)
    {
        await _baseService.CheckDataScope(orgIds);
    }
    
}